package cn.itcast.travel.dao.impl;

import cn.itcast.travel.dao.UserDao;
import cn.itcast.travel.domain.FandV;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public User findByUsername(String username) {
        User user = null;
        try {
            //1.定义sql
            String sql = "select * from tab_user where username = ?";
            //2.执行sql
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username);
        } catch (Exception e) {

        }

        return user;
    }

    @Override
    public void save(User user) {
        //1.定义sql
        String sql = "insert into tab_user(username,password,name,birthday,sex,telephone,email) values(?,?,?,?,?,?,?)";
        //2.执行sql

        template.update(sql, user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getBirthday(),
                user.getSex(),
                user.getTelephone(),
                user.getEmail()
        );
    }

    /* *//**
     * 根据激活码查询用户对象
     * @param code
     * @return
     *//*
    @Override
    public User findByCode(String code) {
        User user = null;
        try {
            String sql = "select * from tab_user where code = ?";

            user = template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),code);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }

        return user;
    }*/

    /* *//**
     * 修改指定用户激活状态
     * @param user
     *//*
    @Override
    public void updateStatus(User user) {
        String sql = " update tab_user set status = 'Y' where uid=?";
        template.update(sql,user.getUid());
    }*/

    /**
     * 根据用户名和密码查询的方法
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User findByUsernameAndPassword(String username, String password) {
        User user = null;
        try {
            //1.定义sql
            String sql = "select * from tab_user where username = ? and password = ?";
            //2.执行sql
            user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
        } catch (Exception e) {

        }

        return user;
    }

    /**
     * 查询水果蔬菜数据语句
     *
     * @return
     */
    @Override
    public List<FandV> FandVfindAll() {
        //1.定义sql
        String sql = "select * from fruitandvegitable";
        //2.执行sql
        List<FandV> findallFV = template.query(sql, new BeanPropertyRowMapper<FandV>(FandV.class));

        return findallFV;
    }

    /**
     * 添加水果蔬菜商品信息
     *
     * @param fandV
     */
    @Override
    public void saveFV(FandV fandV) {
        //1.定义sql
        String sql = "insert into fruitandvegitable(name,amount,price,productionDate,shelflife,manufacturer,phone) values(?,?,?,?,?,?,?)";
        //执行对数据库的操作
        template.update(sql, fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone()
        );
    }

    /**
     * 删除
     *
     * @param fandV
     * @return
     */
    @Override
    public String deFV(FandV fandV) {
        String sql = "delete from fruitandvegitable where id = ?";
        //执行sql
        template.update(sql, fandV.getId());
        return sql;
    }

    /**
     * 修改
     *
     * @param fandV
     */
    @Override
    public void findxiugaiIDFV(FandV fandV) {
        String sql = "update fruitandvegitable  set name = ?,amount =?,price = ?,ProductionDate = ?,Shelflife = ?,manufacturer = ?,phone = ? where id = ? ";
        template.update(sql,
                fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone(),
                fandV.getId()
        );
    }

    /**
     * 查询水产
     * @return
     */
    @Override
    public List<FandV> FandAfindAll() {
        //1.定义sql
        String sql = "select * from aquaticproducts";
        //2.执行sql
        List<FandV> findallFA = template.query(sql, new BeanPropertyRowMapper<FandV>(FandV.class));

        return findallFA;

    }

    /**
     * 存储水产
     * @param fandV
     */
    @Override
    public void saveFA(FandV fandV) {
        //1.定义sql
        String sql = "insert into aquaticproducts(name,amount,price,productionDate,shelflife,manufacturer,phone) values(?,?,?,?,?,?,?)";
        //执行对数据库的操作
        template.update(sql, fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone()
        );
    }

    /**
     * 删除水产
     * @param fandV
     * @return
     */
    @Override
    public String deFA(FandV fandV) {
        String sql = "delete from aquaticproducts where id = ?";
        //执行sql
        template.update(sql, fandV.getId());
        return sql;
    }

    /**
     * 修改水产
     * @param fandV
     */
    @Override
    public void findxiugaiIDFA(FandV fandV) {
        String sql = "update aquaticproducts  set name = ?,amount =?,price = ?,ProductionDate = ?,Shelflife = ?,manufacturer = ?,phone = ? where id = ? ";
        template.update(sql,
                fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone(),
                fandV.getId()
        );
    }

    /**
     * 查询肉类
     * @return
     */
    @Override
    public List<FandV> FandMfindAll() {
        //1.定义sql
        String sql = "select * from freshmeat";
        //2.执行sql
        List<FandV> findallFV = template.query(sql, new BeanPropertyRowMapper<FandV>(FandV.class));

        return findallFV;
    }

    /**
     * 存储肉类
     * @param fandV
     */
    @Override
    public void saveFM(FandV fandV) {
        //1.定义sql
        String sql = "insert into freshmeat(name,amount,price,productionDate,shelflife,manufacturer,phone) values(?,?,?,?,?,?,?)";
        //执行对数据库的操作
        template.update(sql, fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone()
        );
    }

    /**
     * 删除肉类
     * @param fandV
     * @return
     */
    @Override
    public String deFM(FandV fandV) {
        String sql = "delete from freshmeat where id = ?";
        //执行sql
        template.update(sql, fandV.getId());
        return sql;
    }

    /**
     * 修改肉类
     * @param fandV
     */
    @Override
    public void findxiugaiIDFM(FandV fandV) {
        String sql = "update freshmeat  set name = ?,amount =?,price = ?,ProductionDate = ?,Shelflife = ?,manufacturer = ?,phone = ? where id = ? ";
        template.update(sql,
                fandV.getName(),
                fandV.getAmount(),
                fandV.getPrice(),
                fandV.getProductionDate(),
                fandV.getShelflife(),
                fandV.getManufacturer(),
                fandV.getPhone(),
                fandV.getId()
        );
    }

    @Override
    public List<FandV> Fmohuchaxun(FandV fandV) {

        StringBuffer ss = new  StringBuffer();
        String sql1 = "select * from fruitandvegitable where name like ";
        String sql2 = "'%"+ fandV.getName() + "%'";

        ss.append(sql1);
        ss.append(sql2);

        String st = ss.toString();

        return template.query(st,new BeanPropertyRowMapper<FandV>(FandV.class));
    }

    /**
     * 水产模糊查询
     * @param fandV
     * @return
     */
    @Override
    public List<FandV> Amohuchaxun(FandV fandV) {
        StringBuffer ss = new  StringBuffer();
        String sql1 = "select * from aquaticproducts where name like ";
        String sql2 = "'%"+ fandV.getName() + "%'";

        ss.append(sql1);
        ss.append(sql2);

        String st = ss.toString();

        return template.query(st,new BeanPropertyRowMapper<FandV>(FandV.class));
    }

    /**
     * 肉类模糊查询
     * @param fandV
     * @return
     */
    @Override
    public List<FandV> Mmohuchaxun(FandV fandV) {
        StringBuffer ss = new  StringBuffer();
        String sql1 = "select * from freshmeat where name like ";
        String sql2 = "'%"+ fandV.getName() + "%'";

        ss.append(sql1);
        ss.append(sql2);

        String st = ss.toString();

        return template.query(st,new BeanPropertyRowMapper<FandV>(FandV.class));
    }


}
